package com.xindong.paking.utils;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xindong.paking.dtos.OrderDTO;
import com.xindong.paking.dtos.PageResult;
import com.xindong.paking.entity.ParkingLot;
import com.xindong.paking.entity.TbOrder;
import com.xindong.paking.entity.TbUser;
import com.xindong.paking.mapper.ParkingLotMapper;
import com.xindong.paking.service.ParkingLotService;
import com.xindong.paking.service.TbUserService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

/**
 * @Author jielun
 * @Date 2023/2/14 15:33
 * @Version 1.0
 */
@Component
// 将订单对象转为DTO
public class ChangeUtil {
    private volatile HashMap<Integer,String> parkingMap = new HashMap<>();
    private volatile HashMap<Integer,String> userMap = new HashMap<>();
    private volatile HashMap<Integer,String> statusMap = new HashMap<>();

    @Resource
    private ParkingLotMapper parkingLotMapper;
    @Autowired
    private TbUserService tbUserService;

    public List<OrderDTO> changeModel(List<TbOrder> list) {
        //利用多线程获取不同表的基础参数
        CountDownLatch latch = new CountDownLatch(3);
        new Thread(()->{
            // 初始化停车场map
            initParkingList();
            latch.countDown();
        }).start();

       new Thread(()->{
           // 初始化用户
           initUserMap();
           latch.countDown();
       }).start();

        new Thread(()->{
            // 初始化状态表
            initStatusMap();
            latch.countDown();
        }).start();

        try {
            // 等待latch归零
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // 转换
        ArrayList<OrderDTO> orderDTOS = new ArrayList<>();
        for (TbOrder tbOrder : list) {
            OrderDTO orderDTO = new OrderDTO();
            orderDTO.setId(tbOrder.getId());
            orderDTO.setStartTime(tbOrder.getStartTime());
            orderDTO.setEndTime(tbOrder.getEndTime());
            orderDTO.setCarNumber(tbOrder.getCarNumber());
            orderDTO.setPositionId(tbOrder.getPositionId().toString());
            orderDTO.setParkingId(parkingMap.get(tbOrder.getParkingId()));
            orderDTO.setStatus(statusMap.get(tbOrder.getStatus()));
            orderDTO.setUserId(userMap.get(tbOrder.getUserId()));
            orderDTO.setMoney(tbOrder.getMoney());
            if (tbOrder.getDiscountId() != null)
            orderDTO.setDiscountId(tbOrder.getDiscountId().toString());
            orderDTOS.add(orderDTO);
        }
        return orderDTOS;
    }

    private void initUserMap() {
        Result list = tbUserService.list(0, 100);
        PageResult data = (PageResult)list.getData();
        List<TbUser> records = data.getRecords();
        for (TbUser record : records) {
            userMap.put(record.getId(),record.getUsername());
        }
    }

    public void initParkingList(){
        List<ParkingLot> parkingLots = parkingLotMapper.selectList(null);
        for (ParkingLot parkingLot : parkingLots) {
            parkingMap.put(parkingLot.getId(),parkingLot.getName());
        }
    }

    public void initStatusMap() {
        statusMap.put(1, "车辆尚未离开");
        statusMap.put(2, "未支付订单");
        statusMap.put(3, "订单已完成");
    }

    public Page<OrderDTO> changePageModel(Page<TbOrder> pageParam,Integer page,Integer pageSize){
        Page<OrderDTO> orderDTOPage = new Page<>(page, pageSize);
        orderDTOPage.setCurrent(pageParam.getCurrent());
        orderDTOPage.setOrders(pageParam.getOrders());
        orderDTOPage.setPages(pageParam.getPages());
        orderDTOPage.setRecords(this.changeModel(pageParam.getRecords()));
        orderDTOPage.setSize(pageParam.getSize());
        orderDTOPage.setTotal(pageParam.getTotal());
        return orderDTOPage;
    }
}
